﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class RoleBLL
    {
        private static int roleID = 0;
        GaraDataContext DB = new GaraDataContext();
        public IQueryable SelectAll()
        {
            return DB.Roles.Select(n => new
            {
                n.RoleID,
                n.RoleName,
                n.Status
            }).AsQueryable();
        }

        public IQueryable DropListRole()
        {
            return DB.Roles.Select(n => new
            {
                n.RoleID,
                n.RoleName,
                n.Status
            }).Where(n => n.Status == MessageAction.CapPhep).AsQueryable();
        }

        public string Insert(RoleDTO roleDTO)
        {
            try
            {
                Role role = new Role();
                role.RoleID = roleID++;
                role.RoleName = roleDTO.RoleName;
                role.Status = roleDTO.Status;

                DB.Roles.InsertOnSubmit(role);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

            public string update(RoleDTO roleDTO){
                try
                {
                    Role role = DB.Roles.Where(n => n.RoleID == roleDTO.RoleID).FirstOrDefault();
                    role.RoleName = roleDTO.RoleName;
                    role.Status = roleDTO.Status;

                    DB.SubmitChanges();
                    return MessageAction.Success;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    }
}
